<template>
  <el-form :inline="true" :model="formInline" class="demo-form-inline">
    <el-form-item label="采购日期">
      <el-date-picker v-model="formInline.startDate" type="date" placeholder="起始日期" clearable />-<el-date-picker
        v-model="formInline.endDate" type="date" placeholder="结束日期" clearable />
    </el-form-item>

    <el-form-item label="羊只类型" style="width: 160px;">
      <el-select v-model="formInline.sheepType" placeholder="羊只类型" clearable>
        <el-option label="种公" :value="1" />
        <el-option label="种母" :value="2" />
        <el-option label="后裔" :value="3" />
      </el-select>
    </el-form-item>

    <el-form-item label="仓库" style="width: 160px;">
      <el-select v-model="formInline.warehouse" placeholder="仓库" clearable>
        <el-option v-for="item in originList" :label="item.sheepFullName" :value="item.sheepFullName" />
      </el-select>
    </el-form-item>

    <el-form-item label="栋舍" style="width: 160px;">
      <el-select v-model="formInline.buildingId" placeholder="栋舍" clearable>
        <el-option v-for="item in buildingIdList" :label="item.ridgepoleName" :value="item.ridgepoleId" />
      </el-select>
    </el-form-item>

    <el-form-item label="批次/耳号">
      <el-input v-model="formInline.earTag" placeholder="批次/耳号" clearable />
    </el-form-item>

    <el-form-item label="采购员">
      <el-select v-model="formInline.createBy" placeholder="采购员" style="width: 120px" clearable>
        <el-option label="刘永刚" value="刘永刚" />
        <el-option label="孟盘龙" value="孟盘龙" />
        <el-option label="赵秋实" value="赵秋实" />
        <el-option label="牛骞若" value="牛骞若" />
        <el-option label="赵思彤" value="赵思彤" />
        <el-option label="于晋哲" value="于晋哲" />
      </el-select>
    </el-form-item>

    <el-form-item label="单据号">
      <el-input v-model="formInline.document" placeholder="单据号" clearable />
    </el-form-item>

    <el-form-item label="供应商" style="width: 160px;">
      <el-select v-model="formInline.vendorSnowId" placeholder="供应商" clearable>
        <el-option v-for="item in tableDatas" :label="item.vendorName" :value="item.vendorSnowId" />
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="view()">查询</el-button>
      <el-button type="danger" @click="pDel()">批量删除</el-button>
      <el-button type="danger" @click="addpro()">添加</el-button>
    </el-form-item>
  </el-form>
  <div class="app-container">
    <div class="search-container">
      <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="purchaseFormId" label="序号" />
        <el-table-column prop="document" label="单据号" width="150px" />
        <el-table-column prop="dateOfPurchase" label="采购日期" width="150px" />
        <el-table-column prop="sheepType" label="类别">
          <template v-slot="scope">
            <span v-if="scope.row.sheepType == 1">种公</span>
            <span v-else-if="scope.row.sheepType == 2">种母</span>
            <span v-else>后裔</span>
          </template>
        </el-table-column>
        <el-table-column prop="vendorSnowId" label="供应商">
          <template v-slot="scope">
            {{ getVendorName(scope.row.vendorSnowId) }}
          </template>
        </el-table-column>

        <el-table-column prop="summaryEnum" label="摘要">
          <template v-slot="scope">
            {{ "外购转入,内购转入,统购转入,内调转入,直营转出,期初,仓库期初,联营转入".split(",")[scope.row.summaryEnum - 1] }}
          </template>

        </el-table-column>
        <el-table-column prop="earTag" label="批次/耳号" />
        <el-table-column prop="buildingName" label="栋舍" />
        <el-table-column prop="columnName" label="转入栏位" />
        <el-table-column prop="gender" label="性别">
          <template v-slot="scope">
            <span v-if="scope.row.gender == 1">公</span>
            <span v-else>母</span>
          </template>
        </el-table-column>
        <el-table-column prop="buyer" label="采购员" />
        <el-table-column prop="warehouse" label="仓库" />
        <el-table-column prop="goodName" label="商品名称" />
        <el-table-column prop="inboundQuantity" label="入库数量" />
        <el-table-column prop="inboundQuantity" label="调整数量" />
        <el-table-column prop="inboundQuantity" label="结算数量" />
        <el-table-column prop="averageUnitOnly" label="只均单价" />
        <el-table-column prop="amount" label="金额" />
        <el-table-column prop="unitDiscounts" label="单位折扣" />
        <el-table-column prop="settlementAmount" label="结算金额" />
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" width="150px" />
        <el-table-column prop="updateBy" label="修改人" />
        <el-table-column prop="updateTime" label="修改时间" width="150px" />
        <el-table-column fixed="right" label="操作" align="center" width="200px">
          <template #default="scope">
            <el-button link type="danger" size="small" @click="dels(scope.row.purchaseFormId)">删除</el-button>
            <el-button link type="primairy" size="small" @click="upd(scope.row.purchaseFormId)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <el-pagination v-model:current-page="formInline.pageIndex" v-model:page-size="formInline.pageSize"
    :page-sizes="[1, 2, 3, 4]" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"
    @size-change="handleSizeChange" @current-change="handleCurrentChange" />
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import purcure from '@/api/PurchaseMent/index'
import { purcoreQuery, buildingIdQuery } from '@/api/PurchaseMent/model'
import vendor from '@/api/VendorMangMent/index'
import { vendorQuery } from '@/api/VendorMangMent/model'
import { useRouter } from 'vue-router'
import orign, { organQuery } from '@/api/OrganizationalAdministration/index'
const router = useRouter()
onMounted(() => {
  view()
  getVendorList()
  getBuildingIdList()
  getOriginList()
})
//参数
const formInline = reactive<purcoreQuery>({
  "pageIndex": 1,
  "pageSize": 15,
  "startDate": "",
  "endDate": "",
  "sheepType": null,
  "warehouse": "",
  "buildingId": null,
  "earTag": "",
  "createBy": "",
  "document": "",
  "vendorSnowId": null
})
//数据
const tableData = ref([])
const totalCount = ref()
const pageCount = ref()
const view = () => {
  purcure.getList(formInline).then(res => {
    tableData.value = res.data.data
    totalCount.value = res.data.totalCount
    pageCount.value = res.data.pageCount
  })
}
//分页
const handleSizeChange = (val: number) => {
  formInline.pageSize = val
  view()
}
const handleCurrentChange = (val: number) => {
  formInline.pageIndex = val
  view()
}
//删除
const dels = (id: any) => {
  const ids = [id];
  console.log(ids)
  ElMessageBox.confirm(
    '确认要删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    purcure.deletes(ids).then((res: any) => {
      if (res.code > 0) {
        ElMessage.success('删除成功');
        view();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除已取消',
    });
  });
};
const multipleSelection = ref([])
//批量删除
const handleSelectionChange = (val: any) => {
  val.forEach(element => {
    multipleSelection.value.push(element.purchaseFormId)
  });
}
//批量删除
const pDel = () => {
  ElMessageBox.confirm(
    '确认要删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    purcure.deletes(multipleSelection.value).then((res: any) => {
      if (res.code > 0) {
        ElMessage.success('删除成功');
        view();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除已取消',
    });
  });
};

///供应商
//参数
const formInlines = reactive<vendorQuery>({
  venDorCode: "",
  vendorName: "",
  responsiblePhone: "",
  markets: "",
  salesman: "",
  useState: true,
  pageIndex: 1,
  pageSize: 100
})
//数据显示
//总条数
const totalCounts = ref()
const pageCounts = ref()
const tableDatas: any = ref([])
const getVendorList = () => {
  vendor.getList(formInlines).then((res: any) => {
    console.log('完整响应数据:', res);
    tableDatas.value = res.data.data
    totalCounts.value = res.data.totalCount
    pageCounts.value = res.data.pageCount
  })
}
//获取栋舍
const form = reactive<buildingIdQuery>({
  "pageIndex": 1,
  "pageSize": 100,
  "ridgepoleTypeId": 0,
  "ridgepoleName": "",
  "responsiblePerson": ""
})
const buildingIdList: any = ref([])
const getBuildingIdList = () => {
  purcure.getrid(form).then((res: any) => {
    buildingIdList.value = res.data.data
  })
}
//添加
const addpro = () => {
  router.push({
    name: 'SheepProcurementADD'
  })
}
//栋舍名称
const getVendorName = (vendorSnowId: any) => {
  const matchedVendor = tableDatas.value.find(c => c.vendorSnowId === vendorSnowId);
  return matchedVendor ? matchedVendor.vendorName : '';
}
//修改
const upd = (purchaseFormId: any) => {
  router.push({
    name: 'SheepProcirementUpdate',
    query: {
      purchaseFormId: purchaseFormId
    }
  })

}
//羊场
const origin = reactive<organQuery>({
  pageIndex: 1,
  pageSize: 100,
  sheepId: 0,
  sheepFullName: "",
  responsiblePerson: "",
})
const originList: any = ref([])
const getOriginList = () => {
  orign.getSheepFacroryList(origin).then((res: any) => {
    originList.value = res.data.data
  })
}
</script>